﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatterns
{
    interface IMemo
    {
        int GetState();
    }

    class Memo : IMemo
    {
        private readonly int _state;

        public Memo(int state)
        {
            _state = state;
        }

        public int GetState()
        {
            return _state;
        }
    }

    class Founder
    {
        public int state;

        public void DoSomething()
        {
            this.state = new Random().Next(100);
            Console.WriteLine("Founder状态:" + this.state);
        }

        public IMemo Save()
        {
            return new Memo(this.state);
        }

        public void Resotre(int state)
        {
            this.state = state;
            Console.WriteLine("还原后的状态:" + this.state);
        }
    }

    class Manager
    {
        private readonly Founder _founder;
        List<IMemo> memos = new List<IMemo>();

        public Manager(Founder founder)
        {
            _founder = founder;
        }

        public void Backup()
        {
            memos.Add(_founder.Save());
        }


        public void Undo()
        {
            if (!memos.Any())
            {
                Console.WriteLine("没有数据了...");

                return;
            }
            _founder.Resotre(memos.Last().GetState());
            memos.Remove(memos.Last());
        }
    }

}
